$().ready(function() {
	validateRule();
	var type = $("#typeInput").val();
    changeDiv(type);
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});

function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/car/carPriceRule/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			amt : {
				required : true,
				number : true
			}
		},
		messages : {
			amt : {
				required : icon + "金额必填且必须是数字"
			}
		}
	})
}

function changeDiv(type){
    if(type==4){
        $("#fixedFeeDiv").hide();
        $("#dyFeeDiv").show();
    }else{
        $("#dyFeeDiv").hide();
        $("#fixedFeeDiv").show();
    }

}

function dyFeeBtnFun(){
    var ruleName = $("#ruleName").val();

    var id = $("#id").val();
    if(ruleName == ''){
        parent.layer.alert('规则名称不能为空');
        return;
    }
    if(hourIndex != 24){
        parent.layer.alert('请检查时间段是否覆盖全天24小时');
        return;
    }
    var segList = new Array();
    $("#t_body tr").each(function(i){
        var start = $(this).children("td").eq(1).html();
        var end = $(this).children("td").eq(2).html();
        var cash = $(this).children("td").eq(3).html();
        start=start.substring(0,start.length-2);
        end=end.substring(0,end.length-2);
        if(cash == '免费'){
            end = (parseInt(end)/60).toFixed(2);
            cash = 0;
        }
        segList.push({start:start,end:end,cash:cash});
    });

    $.ajaxSetup({
        contentType:"application/json"
    });
    var params={
    	id:id,
        amt:0,
        ruleName:ruleName,
        segList:segList
    };
    $.ajax({
        cache : true,
        type : "POST",
        url : "/car/carPriceRule/updateLs",
        data : JSON.stringify(params),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });
}
function checkMin() {
    var reg =/^\d+$/;　　//正整数
    if (!reg.test($("#freeMinInput").val())) {
        $("#freeMinInput").val(0);
        return false;
    }else{
        var freeMin = parseInt($("#freeMinInput").val());
        if(freeMin > 1440){
            freeMin =1440;
        }
        $("#freeMinInput").val(freeMin);
        hourIndex =(freeMin/60).toFixed(2);
        if(hourIndex != 0){
            $("#usSectionStartTime").val(hourIndex);
            startHour.length = 0;startHour=[];
            endHour.length = 0;endHour=[];
            startHour = [hourIndex];
            var endStart = Math.ceil(startHour);
            for(var i=endStart;i<=24;i++){
                endHour[i-endStart] = i;
            }
        }else{
            $("#usSectionStartTime").val(0);
            startHour = [0];
        }
        $("#usSectionStartTime").hunterTimePicker('',startHour);
        $("#usSectionEndTime").hunterTimePicker('',endHour);
        $("#cashInput").val(0);
        $("#usSectionEndTime").val('');
    }

}
function clearAll(){
    $("#t_body").html('');
    hourIndex = 0;
}
var hourIndex = 24;
var startHour = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
var endHour = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
function  addSegment(){
    var segTableBody = $("#t_body").html();
    if(hourIndex != 0){
        $("#freeMinInput").attr("disabled",true);
        $("#usSectionStartTime").val(hourIndex);
        startHour.length = 0;startHour=[];
        endHour.length = 0;endHour=[];
        startHour = [hourIndex];
        for(var i=hourIndex;i<=24;i++){
            endHour[i-hourIndex] = i;
        }
    }else{
        $("#usSectionStartTime").val(0);
        $("#freeMinInput").removeAttr("disabled");
        startHour = [0];
    }
    $("#usSectionStartTime").hunterTimePicker('',startHour);
    $("#usSectionEndTime").hunterTimePicker('',endHour);
    $("#cashInput").val(0);
    $("#usSectionEndTime").val('');
    $('#segModal').modal("show");
}
function delOne(obj){
    hourIndex = $(obj).parent().parent().children("td").eq(1).html();
    $(obj).parent().parent().remove();
    $("a[class='btn btn-warning']:last").show();
}
function addSegBtn(){
    var startHour = $("#usSectionStartTime").val();
    var endHour = $("#usSectionEndTime").val();
    var cash = $("#cashInput").val();
    var freeMin = $("#freeMinInput").val();
    if(startHour == ''){
        parent.layer.alert('请选择起始时间');
        return;
    }
    if(endHour == ''){
        parent.layer.alert('请选择结束时间');
        return;
    }
    if(endHour == startHour){
        parent.layer.alert('结束时间要大于起始时间');
        return;
    }
    if(cash == ''){
        parent.layer.alert('请输入时间段收费金额');
        return;
    }
    if(cash == 0){
        parent.layer.alert('收费收费金额必须大于零');
        return;
    }
    hourIndex = endHour;
    $('#closeBtn').click();
    $('.btn-warning').css('display','none');
    var segHtml = $("#t_body").html();
    if(freeMin != '0' && segHtml.indexOf("免费")==-1){
        segHtml = segHtml + "<tr><td>【免费时长"+freeMin+"分钟】</td><td>0分钟</td><td>"+freeMin+"分钟</td><td>免费</td><td></td></tr>";
    }
    segHtml = segHtml + "<tr><td>【"+startHour+"小时-"+endHour+"小时】</td><td>"+startHour+"小时</td><td>"+endHour+"小时</td><td>"+cash+"</td><td><a class='btn btn-warning' href='javascript:;' attr="+startHour+" onclick='delOne(this)' title='删除'><i class='fa fa-remove'></i></a></td></tr>";
    $("#t_body").html(segHtml);
}